package com.itheima.practice;


public class Test {

    public static void main(String[] args) {
        //给定一个未排序的整数数组 nums，找到其中最长上升子序列的长度。
        int[] nums = {10, 9, 2, 5, 3, 7, 101, 18};      //最长的上升子序列是 [2,3,7,101]
        int length = lengthOfLIS(nums);
        System.out.println(length);
    }

    public static int lengthOfLIS(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }

        int n = nums.length;
        int[] dp = new int[n];  // 初始化 dp 数组，每个元素默认为 1
        for (int i = 0; i < n; i++) {
            dp[i] = 1;
        }

        for (int i = 1; i < n; i++) {
            for (int j = 0; j < i; j++) {
                if (nums[j] < nums[i]) {
                    dp[i] = Math.max(dp[i], dp[j] + 1);
                }
            }
        }

        int maxLength = 0;
        for (int length : dp) {
            maxLength = Math.max(maxLength, length);
        }

        return maxLength;
    }


}
